<?php
namespace Home\Controller;

class ChartController extends CommonAction
{

    public function EmailController()
    {
        parent::__construct();
    }

    protected function _getNavigator($name, $action)
    {
        $navigator = array(
            array(
                'name' => '任务图表',
                'url'  => '###'
            ),
            array(
                'name' => $name,
                'url'  => $action
            )
        );
        return $navigator;
    }

    public function people_current()
    {
        $dept   = $_SESSION['dept'];
        $userId = $_SESSION['user_id'];
        $dept   = D('Dept', 'Service')->getSpecialUserDept($userId, $dept);

        // 获取CRM团队人员
        $sql0      = 'select zt_user.*,zt_dept.`name` as dept_name from zt_user left join zt_dept on zt_user.dept=zt_dept.id where dept in (' . $dept . ') and deleted="0" and role="dev" order by dept';
        $peopleRes = M()->query($sql0);

        $sql1     = "SELECT
            *
        FROM
            zt_issue
        WHERE
            `status` IN (4, 5)";
        $issueRes = M()->query($sql1);

        $peopleIssueList = array();
        foreach ($issueRes as $issue) {
            $peopleIssueList[$issue['developer']][$issue['id']] = $issue;
            if ($issue['develop_members']) {
                $developMembers = explode(',', $issue['develop_members']);
                foreach ($developMembers as $developMember) {
                    $peopleIssueList[$developMember][$issue['id']] = $issue;
                }
            }
        }
        $date      = $this->_getParam('date', date('Y-m-d'));
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('团队当前工作', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->assign('date', $date);
        $this->assign('peopleList', $peopleRes);
        $this->assign('peopleIssueList', $peopleIssueList);
        $this->display();
    }

    public function team_work_process()
    {
        $dept   = $_SESSION['dept'];
        $userId = $_SESSION['user_id'];
        $dept   = D('Dept', 'Service')->getSpecialUserDept($userId, $dept);

        // 获取团队人员
        $sql0       = 'select zt_user.*,zt_dept.`name` as dept_name from zt_user left join zt_dept on zt_user.dept=zt_dept.id where dept in (' . $dept . ') and deleted="0"';
        $peopleRes  = M()->query($sql0);
        $peopleList = array();
        foreach ($peopleRes as $people) {
            $peopleList[$people['id']] = $people;
        }

        $sql1      = "SELECT
            *
        FROM
            zt_issue
        WHERE
            `status` IN (1, 3, 4, 5, 6)";
        $issueRes  = M()->query($sql1);
        $issueList = array();
        foreach ($issueRes as $issue) {
            if (isset($peopleList[$issue['developer']]) || isset($peopleList[$issue['needer']])) {
                $issueList[] = $issue;
            }
        }

        $sql2       = 'select zt_user.*,zt_dept.`name` as dept_name from zt_user left join zt_dept on zt_user.dept=zt_dept.id where deleted="0"';
        $allPeopleRes  = M()->query($sql2);
        $allPeopleList = array();
        foreach ($allPeopleRes as $people) {
            $allPeopleList[$people['id']] = $people;
        }

        $issueStatusModel = M('IssueStatus');
        $res              = $issueStatusModel->select();
        $statusList       = array();
        foreach ($res as $value) {
            $statusList[$value['status']] = $value;
        }

        $date      = $this->_getParam('date', date('Y-m-d'));
        $sectionId = $this->_getSectionId(__CLASS__, __FUNCTION__);
        $this->assign('navigator', $this->_getNavigator('团队工作进度', '/home/' . $sectionId));
        $this->assign('section', $sectionId);
        $this->assign('date', $date);
        $this->assign('issueList', $issueList);
        $this->assign('peopleList', $allPeopleList);
        $this->assign('statusList', $statusList);
        $this->display();
    }
}
